using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using ImageEffects;
using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")][Description("生成一个Npc，不依赖其它数据")]
    public class LoadIndependentNpc : LatentActionNodeBase
    {
        public override bool allowRoutineQueueing { get { return false; } }

        private ResLoader _resLoader;

        private GameObject _npcGameObject;
        private Animator _animator;
        private LightElement _lightElement;
        
        private bool _loaded = false;
        
        public IEnumerator Invoke(string resPath,string pointName,string lightScene,string animTrigger)
        {
            _resLoader = ResLoader.Alloc();
            
            _resLoader.Add2Load(resPath, ((success, assetName, asset) =>
            {
                _npcGameObject = GameObject.Instantiate(asset as GameObject);
                var point = BlueprintUtils.GetBlueprintPoint(pointName);
                _npcGameObject.transform.position = point.position;
                _npcGameObject.transform.eulerAngles = point.eulerAngles;
                _npcGameObject.SetActive(true);
                _animator = _npcGameObject.GetComponentInChildren<Animator>();
                _lightElement = _npcGameObject.GetComponentInChildren<LightElement>();

                if (!string.IsNullOrEmpty(lightScene) && _lightElement)
                {
                    LightController lightController = BlueprintUtils.GetLightController(lightScene.Trim());
                    if (lightController && _lightElement)
                    {
                        lightController.AddLightElement(_lightElement);
                    }
                }

                if (!string.IsNullOrEmpty(animTrigger) && _animator)
                {
                    _animator.SetTrigger(animTrigger);
                }
                
                _loaded = true;
            }));
            _resLoader.Load();

            while (!_loaded)
            {
                yield return new WaitForOneFrame();
            }
            
        }
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<string>("resPath");
            var p2 = node.AddValueInput<string>("pointName");
            var p3 = node.AddValueInput<string>("lightScene");
            var p4 = node.AddValueInput<string>("animTrigger");
            
            node.AddFlowInput("In", (f) => { Begin(Invoke(p1.value,p2.value,p3.value,p4.value), f); });
            
            node.AddValueOutput("npcGameObject", () => _npcGameObject);
            node.AddValueOutput("animator", () => _animator);
            node.AddValueOutput("lightElement", () => _lightElement);
        }

        public override void OnGraphStoped()
        {
            base.OnGraphStoped();
            if (_npcGameObject)
            {
                GameObject.DestroyImmediate(_npcGameObject);
            }
            
            if (_resLoader!=null)
            {
                _resLoader.Recycle2Cache();
                _resLoader = null;
            }
        }
    }
}